<template>
  <el-container class="tools-container">
    <!-- 顶部导航 -->
    <el-header class="page-header">
      <div class="header-content">
        <div class="header-left">
          <el-icon size="28"><Tools /></el-icon>
          <h2>开发者工具箱</h2>
        </div>
        <div class="header-right">
          <el-button 
            type="primary" 
            size="large" 
            class="back-button"
            @click="handleRedirect"
          >
            <el-icon><Back /></el-icon>返回首页
          </el-button>
        </div>
      </div>
    </el-header>

    <!-- 主要内容区 -->
    <el-main class="page-main">
      <div class="content-wrapper">
        <el-row :gutter="40" justify="center">
          <!-- URL编解码工具 -->
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
            <el-card class="tool-card" shadow="hover">
              <template #header>
                <div class="card-header">
                  <div class="header-left">
                    <el-icon><Link /></el-icon>
                    <span>URL 编解码</span>
                  </div>
                  <el-tooltip content="将字符串转换为URL安全的格式" placement="top">
                    <el-icon><QuestionFilled /></el-icon>
                  </el-tooltip>
                </div>
              </template>
              
              <div class="tool-content">
                <div class="tool-form">
                  <!-- 输入区域 -->
                  <div class="input-section">
                    <div class="section-header">
                      <span class="title">输入内容</span>
                      <el-tooltip content="Ctrl + Enter 快速编码" placement="top">
                        <el-icon><Edit /></el-icon>
                      </el-tooltip>
                    </div>
                    <div class="textarea-wrapper">
                      <el-input
                        v-model="urlInput"
                        type="textarea"
                        placeholder="请输入需要编码/解码的字符串"
                        resize="none"
                        :spellcheck="false"
                        @keydown.ctrl.enter="handleUrlEncode"
                        @keydown.ctrl.shift.enter="handleUrlDecode"
                      />
                    </div>
                  </div>

                  <!-- 按钮组 -->
                  <div class="button-group">
                    <el-button-group>
                      <el-button 
                        type="primary" 
                        size="large"
                        @click="handleUrlEncode"
                        :disabled="!urlInput"
                      >
                        <el-icon><Lock /></el-icon>编码
                      </el-button>
                      <el-button 
                        type="success" 
                        size="large"
                        @click="handleUrlDecode"
                        :disabled="!urlInput"
                      >
                        <el-icon><Unlock /></el-icon>解码
                      </el-button>
                    </el-button-group>
                    
                    <el-button-group>
                      <el-button 
                        type="info" 
                        size="large"
                        @click="handleCopy(urlResult)"
                        :disabled="!urlResult"
                      >
                        <el-icon><CopyDocument /></el-icon>复制
                      </el-button>
                      <el-button 
                        size="large"
                        @click="clearUrl"
                        :disabled="!urlInput && !urlResult"
                      >
                        <el-icon><Delete /></el-icon>清空
                      </el-button>
                    </el-button-group>
                  </div>

                  <!-- 输出区域 -->
                  <div class="output-section">
                    <div class="section-header">
                      <span class="title">输出结果</span>
                      <el-tooltip content="点击复制结果" placement="top">
                        <el-icon><CopyDocument /></el-icon>
                      </el-tooltip>
                    </div>
                    <div class="textarea-wrapper">
                      <el-input
                        v-model="urlResult"
                        type="textarea"
                        placeholder="编码/解码结果"
                        resize="none"
                        readonly
                      />
                    </div>
                  </div>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { 
  Tools, Link, List, Lock, Unlock, CopyDocument, Delete, 
  Back, QuestionFilled, Edit
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// URL编解码相关
const urlInput = ref('')
const urlResult = ref('')


// URL编码
const handleUrlEncode = () => {
  if (!urlInput.value) {
    ElMessage.warning('请输入需要编码的内容')
    return
  }
  try {
    urlResult.value = encodeURIComponent(urlInput.value)
    ElMessage.success('编码成功')
  } catch (error) {
    ElMessage.error('编码失败：' + error.message)
  }
}

// URL解码
const handleUrlDecode = () => {
  if (!urlInput.value) {
    ElMessage.warning('请输入需要解码的内容')
    return
  }
  try {
    urlResult.value = decodeURIComponent(urlInput.value)
    ElMessage.success('解码成功')
  } catch (error) {
    ElMessage.error('解码失败：' + error.message)
  }
}





// 复制结果
const handleCopy = async (text) => {
  if (!text) {
    ElMessage.warning('没有可复制的内容')
    return
  }
  
  try {
    const input = document.createElement('textarea')
    input.value = text
    document.body.appendChild(input)
    input.select()
    document.execCommand('copy')
    document.body.removeChild(input)
    ElMessage.success('复制成功')
  } catch (error) {
    ElMessage.error('复制失败，请手动复制')
  }
}

// 清空URL编解码
const clearUrl = () => {
  urlInput.value = ''
  urlResult.value = ''
  ElMessage.success('已清空')
}

const handleRedirect = () => {
  router.push('/')
}

</script>

<style scoped lang="scss">
.tools-container {
  min-height: 100vh;
  background: linear-gradient(135deg, var(--el-color-primary-light-9) 0%, var(--el-bg-color) 100%);
}

.page-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  backdrop-filter: blur(10px);
  background-color: rgba(255, 255, 255, 0.8);
  border-bottom: 1px solid rgba(var(--el-color-primary-rgb), 0.1);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  
  .header-content {
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 20px;

    .header-left {
      display: flex;
      align-items: center;
      gap: 12px;

      .el-icon {
        color: var(--el-color-primary);
      }

      h2 {
        margin: 0;
        font-size: 24px;
        background: linear-gradient(45deg, var(--el-color-primary), var(--el-color-primary-light-3));
        -webkit-background-clip: text;
        color: transparent;
        font-weight: 600;
      }
    }

    .header-right {
      .back-button {
        display: flex;
        align-items: center;
        gap: 6px;
        padding: 0 20px;
        height: 40px;
        border-radius: 20px;
        transition: all 0.3s ease;
        
        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(var(--el-color-primary-rgb), 0.2);
        }

        .el-icon {
          font-size: 16px;
        }
      }
    }
  }
}

.page-main {
  padding-top: calc(var(--header-height) + 40px);
}

.tool-card {
  height: 100%;
  backdrop-filter: blur(20px);
  background-color: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.5);
  
  .card-header {
    padding: 25px;
    border-bottom: 1px solid var(--el-border-color-light);
    
    .header-left {
      .el-icon {
        font-size: 24px;
      }
      
      span {
        font-size: 20px;
      }
    }
  }
}

.tool-content {
  height: calc(100vh - var(--header-height) - 180px);
  min-height: 600px;
  padding: 30px;
  
  .tool-form {
    height: 100%;
    display: flex;
    flex-direction: column;
    gap: 20px;
  }

  .input-section,
  .output-section {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    
    .section-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 12px;
      
      .title {
        font-size: 16px;
        font-weight: 500;
        color: var(--el-text-color-primary);
      }
      
      .el-icon {
        font-size: 16px;
        color: var(--el-text-color-secondary);
        cursor: pointer;
        
        &:hover {
          color: var(--el-color-primary);
        }
      }
    }
    
    .textarea-wrapper {
      flex: 1;
      min-height: 0;
      
      :deep(.el-textarea) {
        height: 100%;
        display: flex;
        
        .el-textarea__inner {
          flex: 1;
          height: 100% !important;
          padding: 20px;
          font-size: 16px;
          line-height: 1.6;
          font-family: 'Fira Code', monospace;
          border-radius: 12px;
          border: 1px solid var(--el-border-color);
          background-color: var(--el-bg-color);
          transition: all 0.3s ease;
          
          &:hover {
            border-color: var(--el-color-primary);
          }
          
          &:focus {
            border-color: var(--el-color-primary);
            box-shadow: 0 0 0 3px var(--el-color-primary-light-8);
          }
        }
      }
    }
  }

  .button-group {
    display: flex;
    justify-content: space-between;
    padding: 20px 0;
    
    .el-button {
      min-width: 120px;
      height: 44px;
      font-size: 16px;
      border-radius: 8px;
      
      .el-icon {
        font-size: 18px;
        margin-right: 8px;
      }
      
      &:not(:disabled):hover {
        transform: translateY(-2px);
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .page-header {
    .header-content {
      h2 {
        font-size: 20px;
      }
    }
  }
  
  .tool-card {
    .card-header {
      padding: 20px;
    }
  }
  
  .tool-content {
    height: auto;
    min-height: 0;
    padding: 20px;
    
    .input-section,
    .output-section {
      .textarea-wrapper {
        height: 300px;
      }
    }
    
    .button-group {
      flex-direction: column;
      gap: 10px;
      
      .el-button-group {
        display: flex;
        
        .el-button {
          flex: 1;
          min-width: 0;
          height: 40px;
          font-size: 14px;
        }
      }
    }
  }
}
</style> 